﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text.RegularExpressions;

namespace Abhiruchi_Phase1
{
    /// <summary>
    /// Interaction logic for UC_ORG_TaskMaster.xaml
    /// </summary>
    public partial class UC_ORG_TaskMaster : UserControl
    {
        public static string Mode = "";
        public UC_ORG_TaskMaster()
        {
            InitializeComponent();

            lblEditNew.Content = "New";
            LoadEmptyGrid();
            #region Load Basic Details
            DataTable mydtFrCmbSection = ORGTaskMasterDAL.GetDBDetails(6);

            GetTaskIdentity();

            if (mydtFrCmbSection.Rows.Count > 0)
            {
                cmbShowSec.DataContext = mydtFrCmbSection.DefaultView;
                cmbShowSec.DisplayMemberPath = mydtFrCmbSection.Columns["SectionName"].ToString();
                cmbShowSec.SelectedValuePath = mydtFrCmbSection.Columns["SectionId"].ToString();
            }
            cmbShowGen.Items.Add("Select");
            cmbShowGen.Items.Add("Male");
            cmbShowGen.Items.Add("Female");
            cmbShowHours.Items.Add("Select");
            for (int i = 1; i <= 24; i++)
            {
                cmbShowHours.Items.Add(i);
            }
            cmbShowSec.SelectedIndex = -1;
            chkStatus.IsChecked = true;
            BindDataToGrid();
            #endregion
        }
        #region Automatic TaskID Generation
        public void GetTaskIdentity()
        {
            DataTable mydtFrTskIdentity = ORGTaskMasterDAL.GetDBDetails(5);
            if (mydtFrTskIdentity.Rows.Count > 0)
            {
                if (mydtFrTskIdentity.Rows[0][0].ToString() == "0")
                {
                    lblShowTskID.Text = "1";
                }
                else
                {
                    lblShowTskID.Text = (Int32.Parse(mydtFrTskIdentity.Rows[0][0].ToString()) + 1).ToString();
                }
            }           
        }
        #endregion

        #region Clear Form
        private void btnClear_Click(object sender, RoutedEventArgs e)
        {

            GetTaskIdentity();
            cmbShowGen.SelectedIndex = 0;
            cmbShowHours.SelectedIndex = 0;
            cmbShowSec.SelectedIndex = -1;
            txtTaskShowNm.Text = "";
            txtShowPPDay.Text = "";
            txtShowDesc.Text = "";
            chkStatus.IsChecked = true;
            lblEditNew.Content = "New";
            grdDisp.ItemsSource = null;
            LoadEmptyGrid();

        }
        #endregion

        #region Display Selected Section Details
        private void cmbShowSec_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbShowSec.SelectedIndex == -1)
            {
                return;
            }
            grdDisp.ItemsSource = null;
            txtTaskShowNm.Text = ((DataRowView)cmbShowSec.Items[cmbShowSec.SelectedIndex]).Row[1].ToString();
            DataTable mydt = ORGTaskMasterDAL.GetDBDetails(7, ((DataRowView)cmbShowSec.Items[cmbShowSec.SelectedIndex]).Row[1].ToString());
            DataRow dtrow1;
            for (int i = 0; i < 5; i++)
            {
                dtrow1 = mydt.NewRow();                
                dtrow1["Section"] = "";
                dtrow1["TaskName"] = "";
                dtrow1["TaskMatrix"] = "";
                dtrow1["Description"] = "";
                mydt.Rows.Add(dtrow1);
            }
            grdDisp.ItemsSource = mydt;

        }
        #endregion
        public void LoadEmptyGrid()
        {
            DataTable mydtTask = new DataTable();
            DataRow dtrow;
            for (int i = 0; i < 10; i++)
            {
                dtrow = mydtTask.NewRow();
                mydtTask.Rows.Add(dtrow);
            }
            grdDisp.ItemsSource = mydtTask;
        }
        #region Save Details
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {

            if (validation())
            {
                DataTable mydt = new DataTable();
                int isselect = 1;
                if (chkStatus.IsChecked == false)
                {
                    isselect = 0;
                }


                if (Mode == "")
                {

                    mydt = ORGTaskMasterDAL.InsertAndGetDetails(Int32.Parse(((DataRowView)cmbShowSec.Items[cmbShowSec.SelectedIndex]).Row[0].ToString()), txtTaskShowNm.Text, cmbShowGen.Text + "_" + cmbShowHours.Text + "_" + txtShowPPDay.Text, txtShowDesc.Text, isselect, ((DataRowView)cmbShowSec.Items[cmbShowSec.SelectedIndex]).Row[1].ToString(), 8);
                    MessageBox.Show("Details Inserted Sucessfully", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                    BindDataToGrid();                       
                }
                if (Mode == "Update")
                {
                    mydt = ORGTaskMasterDAL.UpdateAndSelectDetails(Int32.Parse(lblShowTskID.Text.ToString()), Int32.Parse(((DataRowView)cmbShowSec.Items[cmbShowSec.SelectedIndex]).Row[0].ToString()), txtTaskShowNm.Text, cmbShowGen.Text + "_" + cmbShowHours.Text + "_" + txtShowPPDay.Text, txtShowDesc.Text, isselect, ((DataRowView)cmbShowSec.Items[cmbShowSec.SelectedIndex]).Row[1].ToString(), 11);
                    MessageBox.Show("Details Updated Sucessfully", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                    Mode = "";
                    BindDataToGrid();                   
                }

                string cmdItem = ((DataRowView)cmbShowSec.Items[cmbShowSec.SelectedIndex]).Row[1].ToString();
                this.btnClear_Click(this, e);
                grdDisp.ItemsSource = null;
                DataTable mydt1 = ORGTaskMasterDAL.GetDBDetails(7, cmdItem);
                grdDisp.ItemsSource = mydt1;                
            }         
        }
        #endregion

        #region  Validation
        public bool validation()
        {
            lblSecName.Foreground = System.Windows.Media.Brushes.Black;
            lblTaskName.Foreground = System.Windows.Media.Brushes.Black;
            lblGender.Foreground = System.Windows.Media.Brushes.Black;
            lblHrs.Foreground = System.Windows.Media.Brushes.Black;
            lblPPday.Foreground = System.Windows.Media.Brushes.Black;
            if (cmbShowSec.SelectedIndex == -1)
            {
                MessageBox.Show("Section Must be Selected", "Section", MessageBoxButton.OK, MessageBoxImage.Warning);
                cmbShowSec.Focus();
                lblSecName.Foreground = System.Windows.Media.Brushes.Red;
                return false;
            }
            else
            {
                lblSecName.Foreground = System.Windows.Media.Brushes.Black;
            }
            if (txtTaskShowNm.Text == "")
            {
                MessageBox.Show("Task Name is required", "Task Name", MessageBoxButton.OK, MessageBoxImage.Warning);
                txtTaskShowNm.Focus();
                lblTaskName.Foreground = System.Windows.Media.Brushes.Red;
                return false;
            }
            else
            {
                lblTaskName.Foreground = System.Windows.Media.Brushes.Black;
            }

            if (cmbShowGen.SelectedIndex == 0)
            {
                MessageBox.Show("Gender Must be Selected", "Gender", MessageBoxButton.OK, MessageBoxImage.Warning);
                cmbShowGen.Focus();
                lblGender.Foreground = System.Windows.Media.Brushes.Red;
                return false;
            }
            else
            {
                lblGender.Foreground = System.Windows.Media.Brushes.Black;
            }
            if (cmbShowHours.SelectedIndex == 0)
            {
                MessageBox.Show("Hours Must be Selected", "Hours", MessageBoxButton.OK, MessageBoxImage.Warning);
                cmbShowHours.Focus();
                lblHrs.Foreground = System.Windows.Media.Brushes.Red;
                return false;
            }            
            else
            {
                lblHrs.Foreground = System.Windows.Media.Brushes.Black;
            }
            if (txtShowPPDay.Text == "")
            {
                MessageBox.Show("Pay per day should be mentioned", "Pay Per Day", MessageBoxButton.OK, MessageBoxImage.Warning);
                txtShowPPDay.Focus();
                lblPPday.Foreground = System.Windows.Media.Brushes.Red;
                return false;
            }
            else
            {
                lblPPday.Foreground = System.Windows.Media.Brushes.Black;
            }
            if (txtShowPPDay.Text != "")
            {
                if (!Regex.Match(txtShowPPDay.Text, @"^[-+]?[0-9]*\.?[0-9]+$").Success)
                {
                    MessageBox.Show(" Please Enter Correct Amount", "Proper Amount", MessageBoxButton.OK, MessageBoxImage.Warning);
                    txtShowPPDay.Clear();
                    txtShowPPDay.Focus();
                    lblPPday.Foreground = System.Windows.Media.Brushes.Red;
                    return false;
                }
                else
                {
                    lblPPday.Foreground = System.Windows.Media.Brushes.Black;
                }
            }
            return true;
        }
        #endregion

        #region DatagridView Activated Row
        private void grdDisp_RowActivated(object sender, Telerik.Windows.Controls.GridView.RowEventArgs e)
        {
            lblEditNew.Content = "Edit";
            Mode = "Update";
            if (((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[0])).Value == null)
            {
                btnClear_Click(this, e);
                return;
            }
            lblShowTskID.Text = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[0])).Value.ToString();
            cmbShowSec.Text = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[1])).Value.ToString();
            txtTaskShowNm.Text = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[2])).Value.ToString();

            string[] str = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[3])).Value.ToString().Split('_');

            cmbShowGen.Text = str[0];
            cmbShowHours.Text = str[1];
            txtShowPPDay.Text = str[2];
            txtShowDesc.Text = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[4])).Value.ToString();

            if ((((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[5])).Value.ToString()) == "No")
            {
                chkStatus.IsChecked = false;
            }
            else
            {
                chkStatus.IsChecked = true;
            }
        }
        #endregion

        #region All section Tasks
        public void BindDataToGrid()
        {
            DataTable GetAllSections = new DataTable();
            GetAllSections = ORGTaskMasterDAL.GetDBDetails(12);
            DataRow dtrow1;
            for (int i = 0; i < 5; i++)
            {
                dtrow1 = GetAllSections.NewRow();
                dtrow1["Section"] = "";
                dtrow1["TaskName"] = "";
                dtrow1["TaskMatrix"] = "";
                dtrow1["Description"] = "";
                GetAllSections.Rows.Add(dtrow1);
            }
            grdAllDisp.ItemsSource = GetAllSections;
        }

        #endregion       


        #region Duplication Checking for TaskName and Task Matrix
        private void txtShowPPDay_LostFocus(object sender, RoutedEventArgs e)
        {
            bool status = false;
            if (cmbShowGen.SelectedIndex != 0 && cmbShowHours.SelectedIndex != 0 && txtShowPPDay.Text != "")
            {
                DataTable mydt = ORGTaskMasterDAL.GetDetailsforValidation(Int32.Parse(((DataRowView)cmbShowSec.Items[cmbShowSec.SelectedIndex]).Row[0].ToString()), 9);
                for (int i = 0; i < mydt.Rows.Count; i++)
                {
                    if (cmbShowGen.Text + "_" + cmbShowHours.Text + "_" + txtShowPPDay.Text == mydt.Rows[i][1].ToString())
                    {
                        status = true;
                    }
                }
                if (status == true)
                {
                    MessageBox.Show(cmbShowGen.Text + "_" + cmbShowHours.Text + "_" + txtShowPPDay.Text + " for " + cmbShowSec.Text + " is already utilized, please choose another Task Matrix ", "Duplicate Task Matrix", MessageBoxButton.OK, MessageBoxImage.Error);
                    cmbShowGen.SelectedIndex = 0;
                    cmbShowHours.SelectedIndex = 0;
                    txtShowPPDay.Text = "";
                    cmbShowGen.Focus();
                }
            }
        }

        private void txtTaskShowNm_LostFocus(object sender, RoutedEventArgs e)
        {
            bool flag = true;
            if (txtTaskShowNm.Text != "")
            {
                DataTable mydt = ORGTaskMasterDAL.GetDetailsforValidation(Int32.Parse(((DataRowView)cmbShowSec.Items[cmbShowSec.SelectedIndex]).Row[0].ToString()), 9);
                for (int i = 0; i < mydt.Rows.Count; i++)
                {
                    if (txtTaskShowNm.Text == mydt.Rows[i][0].ToString())
                    {
                        flag = false;
                    }
                }
            }
            if (flag == false)
            {
                MessageBox.Show(txtTaskShowNm.Text + " for " + cmbShowSec.Text + " is already utilized, please choose another Task Name ", "Duplicate Task Name", MessageBoxButton.OK, MessageBoxImage.Error);
                cmbShowGen.Focus();
                txtTaskShowNm.Clear();
            }
        }
        #endregion

        private void grdAllDisp_RowActivated(object sender, Telerik.Windows.Controls.GridView.RowEventArgs e)
        {
            lblEditNew.Content = "Edit";
            Mode = "Update";
            if (((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[0])).Value == null)
            {
                btnClear_Click(this, e);
                return;
            }
            lblShowTskID.Text = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[0])).Value.ToString();
            cmbShowSec.Text = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[1])).Value.ToString();
            txtTaskShowNm.Text = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[2])).Value.ToString();

            string[] str = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[3])).Value.ToString().Split('_');

            cmbShowGen.Text = str[0];
            cmbShowHours.Text = str[1];
            txtShowPPDay.Text = str[2];
            txtShowDesc.Text = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[4])).Value.ToString();

            if ((((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[5])).Value.ToString()) == "No")
            {
                chkStatus.IsChecked = false;
            }
            else
            {
                chkStatus.IsChecked = true;
            }

        }
    }
}
